<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/30
 * Time: 17:29
 */

namespace App\Model\Backstage;

use App\Model\Account\SystemType;
use App\Model\Base;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class CustomerFollowSourde extends Base
{
    public $table;
    public $room_info_table;
    public $customers_table;
    public $roomfollow_table;
    public $customerfollow_table;


    public function __construct()
    {
        parent::__construct();
        $this->table = 'customer_follow_source_'.$this->branch_id;
//        $this->table = 'customer_follow_source';

         $this->room_info_table = 'room_info_'.$this->branch_id;
//        $this->room_info_table = 'room_info';

                $this->customers_table = 'customers'.'_'.$this->branch_id;
//        $this->customers_table = 'customers';
        $this->roomfollow_table = 'follow'.'_'.$this->branch_id;
        $this->customerfollow_table = 'customer_follow'.'_'.$this->branch_id;

    }

    /** 添加 客源/房源跟进记录提醒
     * @param $params
     * @param $r_id  房源的跟进ID   /   客源的跟进ID
     * @return mixed
     */
//    public function addCustomerFollowSourde($params,$r_id)
//    {
//        $array = array();
//        if($params['cfs_user_id'] == 1){
//            //本人
//            $cfs_user_id =  explode(',',$this->uid);
//        } else {
//            //本部
//            $cfs_user_id = DB::table('user')->where('u_b_id', $this->branch_id)->where('u_d_id','=',$this->store_id)->value(DB::raw('GROUP_CONCAT(u_id) as u_id'));
//            $cfs_user_id = explode(',',$cfs_user_id);
//        }
//            if(isset($params['cf_c_id'])){
//                $cfs_type_id = $params['cf_c_id'];
//            }elseif(isset($params['f_room_id'])){
//                $cfs_type_id = $params['f_room_id'];
//            }
//        foreach($cfs_user_id as $k=>$v) {
//                $array1 = [
//                    'cf_id'              => $r_id,
//                    'cfs_type_id'        => $cfs_type_id,
//                    'cfs_type'           => $params['cfs_type'],
//                    'cfs_reminder_range' => $params['cfs_user_id'],
//                    'cfs_user_id'        => $v,
//                    'cfs_remind_time'    => strtotime($params['cfs_remind_time']),
//                    'cfs_content'        => $params['cfs_content'],
//                ];
//                array_push($array, $array1);
//        }
//
//        return DB::table($this->table)->insert($array);
//    }
    /**
     * @param $params
     * @param $r_id 跟进ID
     * @return mixed
     */
    public function addCustomerFollowSourde($params,$r_id){
        $array = array();
        if($params['cfs_user_id'] == 1){
            //本人
            $cfs_user_id =  explode(',',$this->uid);
        } else {
            //本部
            $cfs_user_id = DB::table('user')->where('u_b_id', $this->branch_id)->where('u_d_id','=',$this->store_id)->value(DB::raw('GROUP_CONCAT(u_id) as u_id'));
            $cfs_user_id = explode(',',$cfs_user_id);
        }
            if(isset($params['cf_c_id'])){
                $cfs_type_id = $params['cf_c_id'];
            }elseif(isset($params['f_room_id'])){
                $cfs_type_id = $params['f_room_id'];
            }
        foreach($cfs_user_id as $k=>$v) {
                $array1 = [
                    'cf_id'              => $r_id,
                    'cfs_type_id'        => $cfs_type_id,
                    'cfs_type'           => $params['cfs_type'],
                    'cfs_user_id'        => $v,
                    'cfs_update_at'     =>time()
                ];
                array_push($array, $array1);
        }

        return DB::table($this->table)->insert($array);
    }

    /**
     *
     *用户登陆完进入系统时获取用户当天提醒
     */
    public function getUserFollow(){
        $map[] = ['cfs_user_id', $this->uid];
        $map[] = ['cfs_remind_time', '<=', time()];
        return DB::table($this->table)->where($map)->get();
    }

    //业务提醒
    //提醒Id 房客源提醒(1房源提醒 2客源提醒)  房客源编号 交易类型 楼盘字典或客户姓名 提醒时间 //
    public function BusinessReminder(){
        $source = DB::table($this->table)
            ->where('cfs_user_id',$this->uid)
            ->where('cfs_deal_with', 2)
            ->select('cfs_id','cf_id','cfs_type_id','cfs_type')
            ->get();


        $connect = [];
        if(sizeof($source) == 0){
            return false;
        }
        $RoomWordBook = new RoomWordbook();

        foreach($source as $key => $value){
            if($value->cfs_type == 1){
                $roomfollow =  DB::table($this->roomfollow_table)
                    ->where('f_id',$value->cf_id)
                    ->where('cfs_remind_time', '<=', time())
                    ->select('cfs_reminder_range','cfs_content',DB::raw('from_unixtime(cfs_remind_time) as cfs_remind_time'))
                    ->first();
                if ($roomfollow) {
                    //房源提醒
                    $array1 = [];
                    $roomInfo = DB::table($this->room_info_table)
                        ->where('id',$value->cfs_type_id)
                        ->where('room_status', '<>', 3)
                        ->select('id','property_number','trading_type','building_id')
                        ->first();
                    $array1['cfs_id'] = $value->cfs_id;
                    $array1['cf_id'] = $value->cf_id;
                    $array1['type'] = $value->cfs_type;
                    $array1['id'] = $roomInfo->id;
                    $array1['cfs_reminder_range'] = $roomfollow->cfs_reminder_range;
                    $array1['connect']= '【房源提醒】'.$roomInfo->property_number.' '.SystemType::getTypeDetail($roomInfo->trading_type).' '.
                        $RoomWordBook->getWordbook($roomInfo->building_id).' '.$roomfollow->cfs_content.' '.$roomfollow->cfs_remind_time;
                    array_push($connect,$array1);
                }
            }elseif($value->cfs_type == 2){
                //客源提醒
                $customerfollow =  DB::table($this->customerfollow_table)
                    ->where('cf_id',$value->cf_id)
                    ->where('cfs_remind_time', '<=', time())
                    ->select('cfs_reminder_range','cfs_content',DB::raw('from_unixtime(cfs_remind_time) as cfs_remind_time'))
                    ->first();
                if ($customerfollow) {
                    $array2 = [];
                    $customerInfo = DB::table($this->customers_table)
                        ->where('c_id',$value->cfs_type_id)
                        ->where('c_status', '<>', 3)
                        ->select('c_id','c_numbering','c_trading_type','c_user_name')
                        ->first();
                    $array2['cfs_id'] = $value->cfs_id;
                    $array2['cf_id'] = $value->cf_id;
                    $array2['type'] = $value->cfs_type;
                    $array2['id'] = $customerInfo->c_id;

                    $array2['cfs_reminder_range'] = $customerfollow->cfs_reminder_range;
                    $array2['connect']= '【客源提醒】'.$customerInfo->c_numbering.' '.SystemType::getTypeDetail($customerInfo->c_trading_type).' '.
                        $customerInfo->c_user_name.' '.$customerfollow->cfs_content.' '.$customerfollow->cfs_remind_time;
                    array_push($connect,$array2);
                }

            }else{
                return false;
            }
        }
        return $connect;
    }
}